package com.javaee.demov2.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javaee.demov2.persistent.entity.Ticket;
import com.javaee.demov2.persistent.mapper.TicketMapper;
import com.javaee.demov2.service.TicketService;
import com.javaee.demov2.utils.bean.PageResult;
import com.javaee.demov2.utils.bean.SysConstants;
import com.javaee.demov2.utils.response.ServiceException;

import com.javaee.demov2.web.dto.ticket.TicketAddDto;
import com.javaee.demov2.web.dto.ticket.TicketQueryDto;
import com.javaee.demov2.web.dto.ticket.TicketUpdateDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
@Slf4j
public class TicketServiceImpl extends ServiceImpl<TicketMapper, Ticket> implements TicketService {

    /**
     * 添加票据
     */
    @Override
    public void add(TicketAddDto ticketAddDto) {
        Ticket ticket = new Ticket();
        // 逻辑判断
        // 票据编号不能重复，假设票据编号具有唯一性，此处进行校验
        if (isExistTicketNumber(ticketAddDto.getTicketNumber())) {
            throw new ServiceException("票据编号已经存在");
        }

        // 将dto数据复制到ticket实体类
        BeanUtils.copyProperties(ticketAddDto, ticket);
        // 可以设置一些默认状态等属性，例如初始状态为未处理等（根据业务需求）
//        ticket.setStatus(SysConstants.UNPROCESSED);
        super.save(ticket);
    }

    /**
     * 复杂条件查询
     */
    @Override
    public PageResult<Ticket> query(TicketQueryDto queryDto) {
        // 创建分页对象
        Page page = queryDto.getPage();

        // 分页查询
        Page<Ticket> result = super.lambdaQuery()
                .like(StringUtils.isNotBlank(queryDto.getTicketNumber()), Ticket::getTicketNumber, queryDto.getTicketNumber())
                .like(StringUtils.isNotBlank(queryDto.getIssuer()), Ticket::getIssuer, queryDto.getIssuer())
                .like(StringUtils.isNotBlank(queryDto.getReceiver()), Ticket::getReceiver, queryDto.getReceiver())
                .eq(queryDto.getType()!= null, Ticket::getType, queryDto.getType())
                .page(page);

        // 将分页查询结果转换为PageResult的形式
        return PageResult.fromPage(result);
    }

    @Override
    public Ticket findByTicketNumber(String ticketNumber) {
        return super.lambdaQuery().eq(Ticket::getTicketNumber, ticketNumber).one();
    }

    /**
     * 更新票据信息
     */
    @Override
    public void update(TicketUpdateDto updateDto) {
        Ticket ticket = new Ticket();
        // 将dto转换为ticket实体类
        BeanUtils.copyProperties(updateDto, ticket);
        super.updateById(ticket);
    }

    @Override
    public boolean isExistTicketNumber(String ticketNumber) {
        return super.lambdaQuery().eq(Ticket::getTicketNumber, ticketNumber).count() > 0;
    }

    @Override
    public List<Ticket> findByStatus(Integer status) {
        return super.lambdaQuery().eq(Ticket::getStatus, status).list();
    }
}